<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">所属型号</label>
                        <div class="layui-input-block">
                            <div name="model_id" id="model_id" required value="0" ></div>
                        </div>
                    </div>

                    <div class="layui-form-item" id="address">
                        <label class="layui-form-label required">点位名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label" id="label_address">点位地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="address" value="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item" id="byte_area" style="display: none">
                        <label class="layui-form-label">字节区</label>
                        <div class="layui-input-block">
                            <div name="byte_area" id="byte_area_array" required value="1"></div>
                        </div>
                    </div>
                    <div class="layui-form-item" id="field_bit" style="display: none">
                        <label class="layui-form-label">字节地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="field_bit_address" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item" id="message" style="display: none">
                        <label class="layui-form-label">自定义报文</label>
                        <div class="layui-input-block">
                            <input type="text" name="message" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item" id="field_type1">
                        <label class="layui-form-label">数据类型</label>
                        <div class="layui-input-block">
                            <div name="type" id="type" value="1" ></div>
                        </div>
                    </div>

                    <div class="layui-form-item" id="encode1" style="display: none">
                        <label class="layui-form-label">报文编码</label>
                        <div class="layui-input-block">
                            <div name="encode" id="encode" value="0"></div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">字段名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="field_name" value="" required lay-verify="required" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">字段类型</label>
                        <div class="layui-input-block">
                            <div name="field_type" id="field_type" value="2" ></div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">创建时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="created_time" id="created_time" required lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>

        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>

        <script>

            // 相关接口
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/iot-signal/select" + location.search;
            const UPDATE_API = "/app/admin/iot-signal/update";

            // 获取数据库记录
            layui.use(["form", "util", "popup"], function () {
                let $ = layui.$;
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    success: function (res) {

                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(value);
                            } else {
                                obj.attr("value", value);
                                obj[0].value = value;
                            }
                        });

                        // 字段 字段类型 type
                        layui.use(["jquery", "xmSelect"], function() {
                            let value = layui.$("#type").attr("value");
                            let initValue = value ? value.split(",") : [];
                            layui.xmSelect.render({
                                el: "#type",
                                name: "type",
                                filterable: true,
                                initValue: initValue,
                                data: [{"value":"1","name":"点位"},{"value":"2","name":"自定义"},{"value":"3","name":"MQTT主题"}],
                                value: "1",
                                model: {"icon":"hidden","label":{"type":"text"}},
                                clickClose: true,
                                radio: true,
                                on: function (data) {
                                    //获取类型的id
                                    const type_id = data.change[0].value;
                                    if (type_id === '1') {
                                        //点位
                                        document.getElementById("address").style.display = "block";
                                        document.getElementById("encode1").style.display = "none";
                                        document.getElementById("message").style.display = "none";
                                        document.getElementById("field_type1").style.display = "block";
                                        document.getElementById("label_address").innerText = "点位地址";
                                        document.getElementById("field_bit").style.display = "block";
                                        document.getElementById("byte_area").style.display = "block";
                                    }else if (type_id === '2') {
                                        document.getElementById("address").style.display = "none";
                                        document.getElementById("encode1").style.display = "block";
                                        document.getElementById("message").style.display = "block";
                                        document.getElementById("field_type1").style.display = "none";
                                        document.getElementById("field_bit").style.display = "none";
                                        document.getElementById("byte_area").style.display = "none";
                                    }else if (type_id === '3'){
                                        document.getElementById("encode1").style.display = "none";
                                        document.getElementById("message").style.display = "none";
                                        document.getElementById("field_type1").style.display = "none";
                                        document.getElementById("address").style.display = "block";
                                        document.getElementById("label_address").innerText = "主题名称";
                                        document.getElementById("field_bit").style.display = "none";
                                        document.getElementById("byte_area").style.display = "none";
                                    }
                                }
                            })
                        });

                        // 字段 所属型号 model_id
                        layui.use(["jquery", "xmSelect", "popup"], function() {
                            layui.$.ajax({
                                url: "/app/admin/iot-device-model/select?format=tree",
                                dataType: "json",
                                success: function (res) {
                                    let value = layui.$("#model_id").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        el: "#model_id",
                                        name: "model_id",
                                        initValue: initValue,
                                        filterable: true,
                                        data: res.data,
                                        value: "0",
                                        model: {"icon":"hidden","label":{"type":"text"}},
                                        clickClose: true,
                                        radio: true,
                                        layVerify: "required",
                                    });
                                    if (res.code) {
                                        layui.popup.failure(res.msg);
                                    }
                                }
                            });
                        });

                        //字节区
                        layui.use(["jquery", "xmSelect"], function () {
                            let value = layui.$("#byte_area_array").attr("value");
                            let initValue = value ? value.split(",") : [];
                            layui.xmSelect.render({
                                el: "#byte_area_array",
                                name: "byte_area_array",
                                filterable: true,
                                initValue: initValue,
                                data: [{"value": "1", "name": "正常"}, {"value": "2", "name": "高字节"}, {"value": "3", "name": "低字节"}],
                                value: "1",
                                model: {"icon": "hidden", "label": {"type": "text"}},
                                clickClose: true,
                                radio: true,
                            })
                        });

                        // 字段 报文编码 encode
                        layui.use(["jquery", "xmSelect"], function() {
                            let value = layui.$("#encode").attr("value");
                            let initValue = value ? value.split(",") : [];
                            layui.xmSelect.render({
                                el: "#encode",
                                name: "encode",
                                filterable: true,
                                initValue: initValue,
                                data: [{"value":"1","name":"HEX"},{"value":"2","name":"UTF8"},{"value":"3","name":"ASCII"}],
                                value: "0",
                                model: {"icon":"hidden","label":{"type":"text"}},
                                clickClose: true,
                                radio: true,
                            })
                        });

                        // 字段 字段类型 field_type
                        layui.use(["jquery", "xmSelect", "popup"], function() {
                            layui.$.ajax({
                                url: "/app/admin/dict/get/modbusDataType",
                                dataType: "json",
                                success: function (res) {
                                    let value = layui.$("#field_type").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        el: "#field_type",
                                        name: "field_type",
                                        initValue: initValue,
                                        filterable: true,
                                        data: res.data,
                                        model: {"icon":"hidden","label":{"type":"text"}},
                                        clickClose: true,
                                        radio: true,
                                        on:function (data) {
                                            //获取数据类型选中的值
                                            const type_value = layui.$("#type").attr("value");
                                            const type_name = data.change[0].name;
                                            if (type_name==='bit' && type_value==='1'){
                                                document.getElementById("field_bit").style.display = "block";
                                                document.getElementById("byte_area").style.display = "block";
                                            }else{
                                                document.getElementById("field_bit").style.display = "none";
                                                document.getElementById("byte_area").style.display = "none";
                                            }
                                            console.log(type_name,type_value)
                                        }
                                    });
                                    if (res.code) {
                                        layui.popup.failure(res.msg);
                                    }
                                }
                            });
                        });

                        // 字段 创建时间 created_time
                        layui.use(["laydate"], function() {
                            layui.laydate.render({
                                elem: "#created_time",
                                type: "datetime",
                            });
                        })


                        // ajax返回失败
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }

                    }
                });
            });

            //提交事件
            layui.use(["form", "popup"], function () {
                // 字段验证允许为空
                layui.form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });
                layui.form.on("submit(save)", function (data) {
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    if (data.field.encode ===null){
                        data.field.encode="";
                    }
                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>

</html>
